# 配置环境变量；如果您已经提前将api-key提前配置到您的运行环境中，可以省略这个步骤
import os
# 选用RolePlay 配置agent
from modelscope_agent.agents.role_play import RolePlay  # NOQA
from tools import *
from modelscope.utils.config import Config
import json
# from modelscope_agent.tools.dashscope_tools import *
os.environ['DASHSCOPE_API_KEY']="sk-548330d34bbf4650875532ce766e978e"
os.environ['AMAP_TOKEN']="b84f785a1aab09acb0e620b5fa94eda6"
tool_cfg_file = './cfg/tool_config.json'

class IndustryAI:

    def __init__(self) -> None:

        self.role_template = '你扮演一个工业设备信息助手，你需要根据用户指定的设备名称，查询相应设备的详细信息。'

        self.llm_config = {  'model': 'qwen-max', 
                        'model_server': 'dashscope'}
        
        # 读取 tool config 文件
        with open(tool_cfg_file, "r") as file: # 读取事件的配置文件
            tool_cfg = json.load(file)
        tool_cfg_dict = [{k:v} for k,v in tool_cfg.items() if v['enable']]  # 修改格式，以匹配 function_list

        # 工具列表
        self.function_list = tool_cfg_dict

        self.bot = RolePlay(
            function_list=self.function_list, 
            llm=self.llm_config, 
            instruction=self.role_template)
    
    def loop(self,):
        print('开始聊天。（输入`quit`来退出。）')
        while True:
            try:
                user_input = input("user: ")
                if user_input.lower() == 'quit':
                    break
                # 机器人运行agent
                response = self.bot.run(user_input)

                text = ''
                for chunk in response:
                    text += chunk

                print("response:=========================================")
                print(text)
            except KeyboardInterrupt:
                break
            except Exception as e:
                print(f'OMG! 发生了错误:{e}')
                break




            
if __name__ == "__main__":
    pass